<template>
  <div id="app">
    <el-dialog :title="itemdata.name?'编辑部门':'新建部门'" :visible.sync="showDailog" @close="hCancel">
      <el-form ref="deptForm" label-width="120px" :model="form" :rules="rules">
        <el-form-item prop="name" label="部门名称">
          <el-input
            v-model="form.name"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门编码" prop="code">
          <el-input
            v-model="form.code"
            style="width: 80%"
            placeholder="1-50个字符"
          />
        </el-form-item>
        <el-form-item label="部门负责人" prop="manager">
          <el-select
            v-model="form.manager"
            style="width: 80%"
            placeholder="请选择"
          >
            <el-option v-for="item in userList" :key="item.id" :value="item.username" :label="item.username" />
          </el-select>
        </el-form-item>
        <el-form-item label="部门介绍" prop="introduce">
          <el-input
            v-model="form.introduce"
            style="width: 80%"
            placeholder="1-300个字符"
            type="textarea"
            :rows="3"
          />
        </el-form-item>
        <el-form-item>
          <el-button
            type="primary"
            size="small"
            @click="hSubmit"
          >确定</el-button>
          <el-button size="small" @click="hCancel">取消</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>
<script>
import { getDepartmentInfo } from '@/api/departments'
// import { valid } from 'node_modules/mockjs/dist/mock'
export default {
  props: ['dailogShow', 'itemdata', 'allCode'],
  data() {
    return {
      showDailog: false,
      fatherID: '',
      form: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      userList: [],
      rules: {
        name: [
          { required: true, message: '请输入部门名称', trigger: ['blur', 'change'] },
          { min: 1, max: 50, message: '长度在 1 到 50 个字符', trigger: ['blur', 'change'] }
        ],
        code: [
          { required: true, message: '请输入部门编码', trigger: ['blur', 'change'] },
          {
            validator: (rule, value, callback) => {
              const test = this.allCode.findIndex(item => item === value)
              if (test !== -1) {
                callback(new Error('编码重复'))
              } else {
                console.log(test)
                callback()
              }
            },
            trigger: ['blur', 'change']
          }
        ],
        manager: [
          { required: true, message: '请选择部门管理者', trigger: ['blur', 'change'] }
        ],
        introduce: [
          { required: true, message: '请输入部门介绍', trigger: ['blur', 'change'] },
          { min: 3, max: 300, message: '长度在 3 到 300 个字符', trigger: ['blur', 'change'] }
        ]
      }
    }
  },
  created() {

  },
  async mounted() {
    console.log(this.itemdata)
    console.log(this.form)
    this.fatherID = this.itemdata.id
    this.form.name = this.itemdata.name
    this.form.code = this.itemdata.code
    this.form.manager = this.itemdata.manager
    this.form.introduce = this.itemdata.introduce

    // 如果是编辑状态,,在所有部门code中移除当前 的code
    if (this.itemdata.name) {
      this.allCode.splice(this.allCode.findIndex(item => item === this.itemdata.code), 1)
      console.log('编辑状态当前code', this.itemdata.code)
      console.log('编辑状态所有的code', this.allCode)
    }
    const res = await getDepartmentInfo()
    console.log('部门详情', res)
    this.userList = res.data.data
    this.showDailog = this.dailogShow
  },
  methods: {
    hSubmit() {
      //   this.dailogShow = false
      this.$refs.deptForm.validate(valid => {
        if (valid) {
          console.log('子组件收到的数据', this.itemdata)
          if (this.itemdata.code && this.itemdata.introduce) {
            this.$emit('changeState', this.fatherID, this.form)
          } else {
            this.$emit('changeState', this.itemdata.pid, this.form, 'isadd')
          }
        }
      })
    },
    hCancel() {
      //   this.dailogShow = false
      this.$emit('changeState')
    }
  }
}
</script>

<style lang='scss' scoped>
// >>>
// /deep/
// ::v-deep
::v-deep .el-dialog {
  width: 86% !important;
}
.el-form {
  width: 75vh;
}
::v-deep .el-dialog__header .el-dialog__headerbtn{
  display: none;
}
</style>
